package com.hui.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.admin.common.UserContext;
import com.hui.admin.common.enums.ErrorCodeEnum;
import com.hui.admin.common.exception.UserException;
import com.hui.admin.dto.request.AuditPageRequestDTO;
import com.hui.admin.dto.request.AuditSaveRequestDTO;
import com.hui.admin.dto.response.AuditListDTO;
import com.hui.admin.dto.response.AuditPageResponseDTO;
import com.hui.admin.entity.AuditDO;
import com.hui.admin.entity.UserDO;
import com.hui.admin.mapper.AuditMapper;
import com.hui.admin.service.AuditService;
import com.hui.admin.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 审核服务实现类
 */
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, AuditDO> implements AuditService {

    @Resource

    private UserService userService;


    @Override
    public AuditPageResponseDTO getAuditList(AuditPageRequestDTO requestDTO) {
        Page<AuditDO> page = new Page<>(requestDTO.getPageNum(), requestDTO.getPageSize());

        LambdaQueryWrapper<AuditDO> queryWrapper = new LambdaQueryWrapper<>();
        if (requestDTO.getTitle() != null) {
            queryWrapper.like(AuditDO::getTitle, requestDTO.getTitle());
        }
        Page<AuditDO> resultPage = baseMapper.selectPage(page, queryWrapper);

        // 转换 DO 到 DTO
        List<AuditListDTO> dtoList = resultPage.getRecords().stream().map(auditDO -> {
            AuditListDTO dto = new AuditListDTO();
            dto.setId(auditDO.getId());
            dto.setTitle(auditDO.getTitle());
            dto.setContent(auditDO.getContent());
            dto.setPublishTime(auditDO.getPublishTime());
            dto.setUserId(auditDO.getUserId());
            dto.setPublisher(auditDO.getPublisher());
            dto.setReason(auditDO.getReason());
            dto.setStatus(auditDO.getStatus());
            return dto;
        }).toList();

        AuditPageResponseDTO result = new AuditPageResponseDTO();
        result.setList(dtoList);
        result.setTotal(resultPage.getTotal());

        return result;
    }

    @Override
    public void createAudit(AuditSaveRequestDTO requestDTO) {
        Long userId = UserContext.getCurrentUserId();
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getId, userId);
        UserDO userDO = userService.getOne(queryWrapper);
        AuditDO result = new AuditDO();
        result.setPublisher(userDO.getName());
        result.setTitle(requestDTO.getTitle());
        result.setContent(requestDTO.getContent());
        result.setUserId(userId);
        result.setReason(result.getReason());
        boolean save = save(result);
        if (!save) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }
    }

    @Override
    public void deleteAudit(Long id) {
        if (id == null) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }

        boolean flag = removeById(id);

        if (!flag) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }
    }


    @Override
    public void deleteAudits(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }
        boolean flag = removeByIds(Arrays.asList(ids));
        if (!flag) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }
    }

    @Override
    public void updateAudit(AuditSaveRequestDTO requestDTO) {
        LambdaQueryWrapper<AuditDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuditDO::getId, requestDTO.getId());
        AuditDO auditDO = getOne(queryWrapper);
        if (auditDO == null) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }
        AuditDO result = BeanUtil.copyProperties(requestDTO, AuditDO.class);
        boolean flag = updateById(result);
        if (!flag) {
            throw new UserException(ErrorCodeEnum.AUDIT_NOT_EXISTS);
        }

    }
}
